package cn.kgc.rbac.service.impl;

import cn.kgc.rbac.common.CodeMsg;
import cn.kgc.rbac.common.ResponseResult;
import cn.kgc.rbac.dto.RolePageDTO;
import cn.kgc.rbac.entity.RolePers;
import cn.kgc.rbac.entity.UserRole;
import cn.kgc.rbac.mapper.RolePersMapper;
import cn.kgc.rbac.mapper.UserRoleMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.rbac.entity.Role;
import cn.kgc.rbac.service.RoleService;
import cn.kgc.rbac.mapper.RoleMapper;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
@Transactional
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService{

    @Autowired(required = false)
    private RoleMapper roleMapper;

    @Autowired(required = false)
    private RolePersMapper rolePersMapper;
    @Autowired(required = false)
    private UserRoleMapper userRoleMapper;

    @Override
    public ResponseResult selectByPage(RolePageDTO rolePageDTO) {

        Page<Role> rolePage = new Page<>(rolePageDTO.getPage(), rolePageDTO.getLimit());

        QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
        roleQueryWrapper.eq(!StringUtils.isNullOrEmpty(rolePageDTO.getRoleName()), "role_name", rolePageDTO.getRoleName());
        roleMapper.selectPage(rolePage,roleQueryWrapper);

        long total = rolePage.getTotal();
        List<Role> records = rolePage.getRecords();

        return new ResponseResult(CodeMsg.SUCCESS,records,total);
    }

    @Override
    public ResponseResult selectPermissionByRoleId(Integer roleId) {

        QueryWrapper<RolePers> rolePersQueryWrapper = new QueryWrapper<>();
        rolePersQueryWrapper.eq("role_id", roleId);
        List<RolePers> rolePers = rolePersMapper.selectList(rolePersQueryWrapper);

        List<Integer> permissionIds = rolePers.stream().map(rolePers1 -> {
            return rolePers1.getPerId();
        }).collect(Collectors.toList());

        return new ResponseResult(CodeMsg.SUCCESS,permissionIds,null);
    }

    @Override
    public ResponseResult addRolePermission(Integer roleId, String permissionIds) {

        //根据角色id删除权限id  role_per
        QueryWrapper<RolePers> rolePersQueryWrapper = new QueryWrapper<>();
        rolePersQueryWrapper.eq("role_id", roleId);

        rolePersMapper.delete(rolePersQueryWrapper);

        String[] perIds = permissionIds.split(",");

        for (String perId : perIds) {
            RolePers rolePers = new RolePers();
            rolePers.setPerId(Integer.valueOf(perId));
            rolePers.setRoleId(roleId);
            rolePersMapper.insert(rolePers);
        }

        return new ResponseResult(CodeMsg.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(String ids) {

        // 删除的角色是否被占用
        String[] strIds = ids.split(",");
        List<Integer> collect = Arrays.asList(strIds).stream().map(id -> {
            return Integer.valueOf(id);
        }).collect(Collectors.toList());

        QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
        userRoleQueryWrapper.in("role_id", collect);
        List<UserRole> userRoles = userRoleMapper.selectList(userRoleQueryWrapper);

        if(userRoles.size()>0){
            return new ResponseResult(CodeMsg.ROLE_HASUSED);
        }

        // 删除角色信息
        roleMapper.deleteBatchIds(collect);
        // 删除角色和权限关联数据
        QueryWrapper<RolePers> rolePersQueryWrapper = new QueryWrapper<>();
        rolePersQueryWrapper.in("role_id", collect);

        rolePersMapper.delete(rolePersQueryWrapper);

        return new ResponseResult(CodeMsg.SUCCESS);
    }
}




